package org.bdware.irs.backend.controller;


import org.bdware.irs.backend.dto.Organization;
import org.bdware.irs.backend.dto.ResponseWrapper;
import org.bdware.irs.backend.servicesImpl.GrsService;
import org.bdware.irs.backend.servicesImpl.IRHelperServiceImpl;
import org.bdware.irs.backend.utils.DOIParser;
import com.nimbusds.jose.JOSEException;
import org.bdware.bdoa.authentication.annotation.AuthRequired;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * GIRS模块
 * @RestController
 * @RequestMapping(value = "/api/grs")
 */
 @RestController
 @RequestMapping(value = "/api/grs")
public class GRSController {

    @Autowired
    private IRHelperServiceImpl irHelperService;

    @Autowired
    private GrsService grsService;


    @RequestMapping(value = "/generateKey",method = RequestMethod.GET)
    public ResponseWrapper generateKey(String  doi) throws JOSEException {
        return new ResponseWrapper(irHelperService.keyGenerator(doi));
    }

    @RequestMapping(value = "/getInfo",method = RequestMethod.GET)
    public ResponseWrapper<Organization> getInfo(String doi) throws JOSEException {
        return new ResponseWrapper(grsService.getOrganization(doi));
    }

    @RequestMapping(value = "/generatePrefix",method = RequestMethod.GET)
    public ResponseWrapper generatePrefix() {
        return new ResponseWrapper(irHelperService.prefixGenerator());
    }

    @RequestMapping(value = "/saveInfo",method = RequestMethod.POST)
    public ResponseWrapper saveInfo(@RequestBody Organization organization) {
        grsService.saveOrganization(organization);
        return new ResponseWrapper("保存成功");
    }

    @RequestMapping(value = "/deleteOrg",method = RequestMethod.POST)
    public ResponseWrapper deleteOrg(@RequestBody Organization organization) {
        grsService.deleteOrganization(organization);
        return new ResponseWrapper("删除成功");
    }


    @RequestMapping(value = "/approve",method = RequestMethod.POST)
    public ResponseWrapper approve(@RequestBody Organization organization) {
        organization.setStatus("审核通过");
        grsService.saveOrganization(organization);
        return new ResponseWrapper("保存成功");
    }


    @RequestMapping(value = "/disapprove",method = RequestMethod.POST)
    public ResponseWrapper disapprove(@RequestBody Organization organization) {
        organization.setStatus("待审核");
        grsService.saveOrganization(organization);
        return new ResponseWrapper("保存成功");
    }

    @RequestMapping(value = "/applyForPrefix",method = RequestMethod.POST)
    public ResponseWrapper applyForPrefix(@RequestBody Organization organization) {
        organization.setStatus("待审核");
        grsService.saveOrganization(organization);
        return new ResponseWrapper("保存成功");
    }


    @AuthRequired(required = true)
    @RequestMapping(value = "/updateOrganization",method = RequestMethod.POST)
    public ResponseWrapper updateOrganization(@RequestBody Organization organization) {
        grsService.updateOrganization(organization);
        return new ResponseWrapper("保存成功");
    }

    @RequestMapping(value = "/all",method = RequestMethod.GET)
    public ResponseWrapper<List<Organization>> allOrganizations() {
        return new ResponseWrapper(grsService.getAllOrganization());
    }


    /**  LHS启动后连接GRS，更新报运行信息（服务地址、状态等），LHS启动有个心跳机制，定时更新？
     *
     * @param organization，只需要 prefix， status, serviceAddress, key？验证身份
     * @return
     */
    @RequestMapping(value = "/lrsJoin", method = RequestMethod.POST)
    public ResponseWrapper lrsJoin(@RequestBody Organization organization) {
        Organization old = grsService.getOrganization(DOIParser.getAdminDOI(organization.getPrefix()));
        //签名验证

        //更新状态信息
        if(organization.getStatus() != null){
            old.setStatus(organization.getStatus());
        }
        if(organization.getServiceAddress() != null){
            old.setServiceAddress(organization.getServiceAddress());
        }
        return new ResponseWrapper(grsService.updateOrganization(old));

    }

    //解析前缀，返回信息
    @RequestMapping(value = "/resolve", method = RequestMethod.GET)
    public ResponseWrapper resolve(String doi) {
        //查看缓存信息
        //返回离do最近的irs服务器信息
        String adminDOI =DOIParser.getAdminDOI(doi);
        Organization organization = grsService.getOrganization(adminDOI);
        return new ResponseWrapper(organization.toStateInfoBase());
    }
}
